package com.dianrui.qiyouriding.wxapi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;
import com.dianrui.qiyouriding.activity.MainActivity;
import com.dianrui.qiyouriding.activity.PaySuccessActivity;
import com.dianrui.qiyouriding.event.CoopertionEnd;
import com.dianrui.qiyouriding.event.CouponEnd;
import com.dianrui.qiyouriding.event.DepositEnd;
import com.dianrui.qiyouriding.event.OpenOrClose;
import com.dianrui.qiyouriding.event.RechargeEnd;
import com.dianrui.qiyouriding.event.RfreshOrder;
import com.dianrui.qiyouriding.event.RfreshPersionInfos;
import com.dianrui.qiyouriding.event.RfreshWallet;
import com.dianrui.qiyouriding.event.RreshMy;
import com.dianrui.qiyouriding.event.violationEnd;
import com.dianrui.qiyouriding.event.violatonInfoChanged;
import com.dianrui.qiyouriding.event.violatonListChanged;
import com.dianrui.qiyouriding.util.SPUtils;
import com.dianrui.qiyouriding.util.ToastUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

//必须实现接口，重写方法
@SuppressWarnings("ALL")
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;
    public SPUtils spUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        api = WXAPIFactory.createWXAPI(this, Constant.WXAppID, true);
        spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        api.registerApp(Constant.WXAppID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                goToShowMsg((ShowMessageFromWX.Req) req);
                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
                break;
            default:
                break;
        }

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            int code = baseResp.errCode;
            switch (code) {
                case 0:
                    Log.e("微信支付结果--->:", "支付成功");
                    PayResp resp = (PayResp) baseResp;
                    String payType = resp.extData;//在收到支付回调的页面做判断
                    Log.e("微信支付页面类型--->:", payType);
                    switch (payType) {
                        case "orderPay"://订单支付
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new RfreshOrder());//刷新我的行程
                            EventBus.getDefault().post(new OpenOrClose(false));//主页的底部图片变成扫码用车
                            Intent paySuccessintent = new Intent(WXPayEntryActivity.this, PaySuccessActivity.class);
                            startActivity(paySuccessintent);
                            finish();
                            break;
                        case "DepositPay"://押金缴纳
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new DepositEnd());//finish掉
                            EventBus.getDefault().post(new RfreshWallet());
                            EventBus.getDefault().post(new RfreshPersionInfos());

                            Intent intent = new Intent(WXPayEntryActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                            break;

                        case "RechargePay"://充值
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new RfreshWallet());
                            EventBus.getDefault().post(new RechargeEnd());
                            finish();
                            break;

                        case "violationPay"://违章处理
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new violationEnd());
                            EventBus.getDefault().post(new violatonListChanged());
                            EventBus.getDefault().post(new violatonInfoChanged());
                            finish();
                            break;

                        case "CouponPay"://购买优惠券
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new CouponEnd());
                            break;
                        case "PartnerPay"://合伙人计划
                            ToastUtil.showToast("支付成功");
                            finish();
                            break;
                        case "coopertionPay"://申请合同签约
                            ToastUtil.showToast("支付成功");
                            EventBus.getDefault().post(new CoopertionEnd());
                            EventBus.getDefault().post(new RreshMy());
                            finish();
                            break;

                            default:
                                break;

                    }
                    overridePendingTransition(0,0);
                    break;

                case -1:
                    //支付失败
                    Toast.makeText(WXPayEntryActivity.this, "支付失败了", Toast.LENGTH_SHORT).show();
                    finish();
                    break;

                case -2:
                    //支付取消
                    Toast.makeText(WXPayEntryActivity.this, "用户支付取消了", Toast.LENGTH_SHORT).show();
                    finish();
                    break;

                    default:
                        break;

            }
            overridePendingTransition(0,0);
        }
    }


    private void goToShowMsg(ShowMessageFromWX.Req showReq) {
        WXMediaMessage wxMsg = showReq.message;
        WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;

        StringBuilder msg = new StringBuilder();
        msg.append("description: ");
        msg.append(wxMsg.description);
        msg.append("\n");
        msg.append("extInfo: ");
        msg.append(obj.extInfo);
        msg.append("\n");
        msg.append("filePath: ");
        msg.append(obj.filePath);

    }



    public void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }
}